home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c,comp.std.c,comp.lang.c++
- Subject: Re: Floating Point arithmetic problem
- Date: 15 Feb 1996 02:05:35 -0500
- Organization: Montclair State University
- Message-ID: <harmon.824366374@pegasus.montclair.edu>
- References: <c968da6jzm.fsf@damayanti.india.ti.com>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- kuntal@india.ti.com (Kuntal Shah) writes:
- >This addition usually results in a few insignificant digits getting
- >accumulated in my double variable. For eg.
-
- > f = 12.99 is represented as 12.9900000000000002131628207
- > f = 11.111 is represented as 11.1110000000000006536993169
-
- >when the accrued value exceeds 0.0001 which results in failure of the
- >if condition in the above block of code, when ideally no such thing
- >should have occurred.
-
- >I have a few options to overcome this problem :-
-
- >* After each addition, covert 'd' to an unsigned long after
- > multiplying by say 1e8, (thus truncating the unnecessary digits),
- > and divide it by 1e8 to get back the original value.
-
- >* After every few additions, say 1000, do the above operation.
-
- >In both the above operations, a severe problem would arise in cases
- >when the value represented is less than the value asked for. For
- >example,
-
- > f= 213.22 would be represented as 213.2199999999999988631316228
-
- I would follow the approach, essentially, you mention above about doing
- a truncation operation every 1,000 or so additions. However, I wouldn't
- convert to an unsigned long int to do so; I doubt the value early into your
- program could be contained in a long int, much less after multiplying by 1e8!
-
- >* Is it possible to set to zero, say the last 10-15 digits of the
- > decimal part without any effect in the long run on the 5 digit
- > precision I require?
-
- In a set of tests I just ran on my machine (your mileage may vary), I
- had no problem with the use of the ceil() and floor() in effectively rounding
- doubles, and zeroing the late digits. I'm not sure of what the standard
- guarantees for their precision.
-
- When epsilon is a power of 10, say 0.0001, for positive n you could try
- epsilon * floor( d * (1.0 / epsilon)); and that might be all you need.
-
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... 2 + 2 = Ask My Calculator.
-